package com.example.hw;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class Shop extends SQLiteOpenHelper {


	private static final int VERSION = 1;//資料庫版本 

	//建構子
	public Shop(Context context, String name, CursorFactory factory,int version) {
		//context 	哪隻Java要打開這個資料庫
		//name		資料庫名稱
		//factory 	指遊標，一般預設null
		//version	資料庫版本，預設開始為1　　
		super(context, name, factory, version);
	}

	public Shop(Context context,String name) {
		this(context, name, null, VERSION);
	}

	public Shop(Context context, String name, int version) { 
		this(context, name, null, version); 
	} 

	//輔助類建立時運行該方法
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		
		String DATABASE_CREATE_TABLE1 =
				"create table orders("
						+ "order_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL,"
						+ "date VARCHAR"
						+ ");";
		
		String DATABASE_CREATE_TABLE2 =
				"create table orderDetail("
						+ "orderDetail_id INTEGER NOT NULL,"
						+ "order_id INTEGER,"
						+ "product_id INTEGER,"
						+ "number INTEGER,"
						+ "price INTEGER," 
						+ "PRIMARY KEY (orderDetail_id,order_id )"
						+ ");";
		String DATABASE_CREATE_TABLE3 =
				"create table products("
						+ "product_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL,"
						+ "drinkName VARCHAR,"
						+ "price INTEGER,"
						+ "date VARCHAR"
						+ ")";
		
		db.execSQL(DATABASE_CREATE_TABLE1);
		db.execSQL(DATABASE_CREATE_TABLE2);
		db.execSQL(DATABASE_CREATE_TABLE3);
	}
	

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//oldVersion=舊的資料庫版本；newVersion=新的資料庫版本
		db.execSQL("DROP TABLE IF EXISTS orders"); 		
		db.execSQL("DROP TABLE IF EXISTS orderDetail"); 	
		db.execSQL("DROP TABLE IF EXISTS products"); 	
		onCreate(db);
	}

	@Override   
	public void onOpen(SQLiteDatabase db) {    
		super.onOpen(db);      
		// TODO 每次成功打開數據庫後首先被執行    
	}

	@Override
	public synchronized void close() {
		super.close();
	}

}


